package sc.love.boot.es;

import com.alibaba.fastjson.JSONObject;
import sc.love.boot.util.HttpClientUtil;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class MultithreadUploadMessage extends Thread {

    private int startRownum;

    private int endRownum;

    private Connection connection = null;

    public MultithreadUploadMessage(int startRownum, int endRownum, Connection connection) {
        super();
        this.startRownum = startRownum;
        this.endRownum = endRownum;
        this.connection = connection;
    }

    public void run() {
        Statement statement = null;
        ResultSet rs = null;
        StringBuffer sb = new StringBuffer();
        if (connection != null) {
            try {
                // 读取数据
                statement = connection.createStatement();
                String sql = " select c.* from (" + " select rownum rn,a.* " +
                        "from MSGCENTER.message a ) c "
                        + "where c.rn between " + startRownum + " and " + endRownum + " order by c.msg_id";
                rs = statement.executeQuery(sql);
                while (rs.next()) {
                    Message message = new Message();
                    int msg_id = rs.getInt("MSG_ID");
                    message.setMsgId(msg_id);
                    message.setAppCode(rs.getString("APP_CODE"));
                    message.setMsgSeq(rs.getString("MSG_SEQ"));
                    message.setPublishUserId(rs.getLong("PUBLISH_USER_ID"));
                    message.setBatchId(rs.getInt("BATCH_ID"));
                    message.setTitle(rs.getString("TITLE"));
                    message.setSource(rs.getShort("SOURCE"));
                    message.setType(rs.getString("TYPE"));
                    message.setMsgLevel(rs.getString("MSG_LEVEL"));
                    message.setCategory(rs.getShort("CATEGORY"));
                    message.setFixed(rs.getShort("FIXED"));
                    message.setFixedExp(rs.getString("FIXED_EXP"));
                    message.setFixedTime(rs.getDate("FIXED_TIME"));
                    message.setDimension(rs.getString("DIMENSION"));
                    message.setPublishStatus(rs.getShort("PUBLISH_STATUS"));
                    message.setPublishNum(rs.getShort("PUBLISH_NUM"));
                    message.setRemark(rs.getString("REMARK"));
                    Date create_time = rs.getDate("CREATE_TIME");
                    message.setCreateTime(create_time);
                    message.setPushedTime(rs.getDate("PUSHED_TIME"));
                    message.setMsgGroup(rs.getShort("MSG_GROUP"));
                    message.setSmsNotify(rs.getShort("SMS_NOTIFY"));

                    String s = JSONObject.toJSONString(message);
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    String format = simpleDateFormat.format(create_time);
                    String index = "crm-ubp-message-"+format;
                    System.out.println(s);
                    try {
                        String documentByManually = createDocumentByManually("", "", "Basic ZWxhc3RpYzplczQzMjE=", index, "message", msg_id + "", s);
                        System.out.println(msg_id+":"+documentByManually);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {

                // try {
                // if (rs != null)
                // rs.close();
                // if (statement != null)
                // statement.close();
                // if (connection != null)
                // connection.close();
                // } catch (SQLException e) {
                // e.printStackTrace();
                // }

            }
        }
    }

    public static final int outTimes = 3000;
    public static String createDocumentByManually(String ip,String port,String password,String index,String name,String id,String message) throws Exception{
        String url = "http://132.252.128.143:9200/"+index;

        if(name!=null){
            url = url + "/"+name;
            if(id!=null){
                url = url + "/"+id;
            }
        }
        Map headDatas = new HashMap();
        if(password!=null&&!"".equals(password)){
            headDatas.put("Authorization",password);
        }
        String returnMsg = HttpClientUtil.doPost(url, message, "UTF-8", outTimes, headDatas);
        JSONObject returnJson =  JSONObject.parseObject(returnMsg);
        if(returnJson.getJSONObject("error")!=null && !returnJson.getJSONObject("error").isEmpty()){
            throw new Exception(returnJson.getJSONObject("error").toJSONString());
        }
        return returnMsg;
    }

}
